
function LeaveObject() {
	var _id;
	var _caption;
	var _html;
	
	return { 
		id : function(id) { 
			if(arguments.length == 0) {
				return _id;
			} else { 
				_id = id; 
				return this;
			} 
		},  
		caption : function(caption) { 
			if(arguments.length == 0) {
				return _caption;
			} else { 
				_caption = caption; 
				return this;
			} 
		}, 
		html : function(html) { 
			if(arguments.length == 0) {
				return _html;
			} else { 
				_html = html; 
				return this;
			} 
		} 
	} 
}


function Container() {
	var _id;
	var _header; 
	var _content; 
	var _footer; 
	var _copyright; 
	var _html;
	var _leftContainer;
	var _rightContainer;
	
	return { 
		id : function(id) { 
			if(arguments.length == 0) {
				return _id;
			} else { 
				_id = id; 
				return this;
			} 
		},  
		header : function(header) {
			if(arguments.length == 0) {
				return _header;
			} else { 
				_header = header; 
				return this;
			} 
		},
		
		content : function(content) {
			if(arguments.length == 0) {
				return _content;
			} else { 
				_content = content; 
				return this;
			} 
		},
		footer : function(footer) {
			if(arguments.length == 0) {
				return _footer;
			} else { 
				_footer = footer; 
				return this;
			} 
		},
		copyright : function(copyright) {
			if(arguments.length == 0) {
				return _copyright;
			} else { 
				_copyright = copyright; 
				return this;
			} 
		}, 
		leftContainer : function(leftContainer) {  
			if(arguments.length == 0) {
				return _leftContainer;
			} else {  
				_leftContainer = leftContainer; 
				return this;
			} 
		}, 
		rightContainer : function(rightContainer) { 
			if(arguments.length == 0) {
				return _rightContainer;
			} else { 
				_rightContainer = rightContainer; 
				return this;
			} 
		}, 
		moveLeftContainer : function() { 
			if(_leftContainer != null) { 
				$(_html).hide("slide", {direction:'right'}, 500); 
				$("#" + _leftContainer.id()).show("slide", {direction:'left'}, 500); 
			} 
		}, 
		
		moveRightContainer : function() {  
			if(_rightContainer != null) {
				$(_html).hide("slide", {direction:'left'}, 500); 
				$("#" + _rightContainer.id()).show("slide", {direction:'right'}, 500); 
			} 
		}, 
		
		make : function() {
			var html = "";			
			html = html + "<div id='" + _id + "' class='container'/>"; 	
			_html = $(html);
		}, 
		
		build : function() {  
			this.make();   
		},
		
		render : function() {
			this.build();
			if(_header    != null) { $(_html).append(_header.build());}
			if(_content   != null) { $(_html).append(_content.build());}
			if(_footer    != null) { $(_html).append(_footer.build());}
			if(_copyright != null) { $(_html).append(_copyright.build());}				
			$(_html).appendTo("body");   		
			return this;
		},
		
		
		hide : function() {
			$(_html).css("display", "none");
			return this;
		},
		
		show : function() {
			$(_html).css("display", "");
			return this;
		}		
	};	 
}


function Header() {
	var _id;
	var _caption;
	var _left  = [];
	var _right = [];
	var _html;
	
	return {
		id : function(id) { 
			if(arguments.length == 0) {
				return _id;
			} else { 
				_id = id; 
				return this;
			} 
		},  
	
		caption : function(caption) { 
			if(arguments.length == 0) {
				return _caption;
			} else { 
				_caption = caption; 
				return this;
			} 
		},
		
		addLeft  : function(object) { 
			var index = 0; 
			if(_left != null) index = _left.length;
			_left[index] = object; 
			
			return this;
		},
		
		addRight : function(object) {
			var index = 0; 
			if(_right != null) index = _right.length;  
			_right[index] = object; 
			return this;			
			
		},
		
		make : function() {
			var html = "";			
			html = html + "<div id='" + _id + "' class='header'>";
			html = html +	"<div class='nav-top'>"; 
			html = html + 		"<div class='nav-top-left'/>"; 
			html = html + 		"<div class='nav-top-center'>";
			html = html + 	    _caption; 			
		 	html = html + 	    "</div>"; 			
			html = html + 		"<div class='nav-top-right'/>"; 
		 	html = html + 	"</div>"; 
			html = html + "</div>"; 
			_html = $(html); 
			
			$.each(_left, function(columnIndex, columnData) { 
				$(_html).find("div.nav-top-left").append(columnData.build());
			});
			$.each(_right, function(columnIndex, columnData) {   
				$(_html).find("div.nav-top-right").append(columnData.build());
			});
		},
		
		build : function() { 
			this.make();   
			return _html;
		}
	};
}

function Content() {
	var _id;
	var _caption;
	var _html;
	var _add = [];
	
	return {
		id : function(id) { 
			if(arguments.length == 0) {
				return _id;
			} else { 
				_id = id; 
				return this;
			} 
		},  
	
		caption : function(caption) { 
			if(arguments.length == 0) {
				return _caption;
			} else { 
				_caption = caption; 
				return this;
			} 
		},
		
		add : function (add) {   
			var index = 0;
			
			if(_add != null) index = _add.length;  
			_add[index] = add;  
			
			return this; 
		},
		
		make : function() {
			
			var html = "";			
			html = html + "<div id='" + _id + "'  class='content'>"; 
			html = html + "</div>";			
			_html = $(html); 
			
			
			$.each(_add, function(columnIndex, columnData) {  
				_html.append(columnData.build());
			});
		},
		
		build : function() {
			this.make(); 
			return _html;
		}
	};
}


function Footer() {
	var _id;
	var _caption;
	var _html;
	
	return {
		id : function(id) { 
			if(arguments.length == 0) {
				return _id;
			} else { 
				_id = id; 
				return this;
			} 
		},  
	
		caption : function(caption) { 
			if(arguments.length == 0) {
				return _caption;
			} else { 
				_caption = caption; 
				return this;
			} 
		},
		
		make : function() {
			var html = "";			
			html = html + "<div id='" + _id + "' class='footer'>"; 
			html = html + "</div>";
			_html = $(html); 
		},
		build : function() {
			this.make(); 
			return $(_html);
		}
	};
}



function Copyright() {
	var _id;
	var _caption;
	var _html;
	
	return {
		id : function(id) { 
			if(arguments.length == 0) {
				return _id;
			} else { 
				_id = id; 
				return this;
			} 
		},  
	
		caption : function(caption) { 
			if(arguments.length == 0) {
				return _caption;
			} else { 
				_caption = caption; 
				return this;
			} 
		},
		
		make : function() {
			var html = "";			
			html = html + "<div id='" + _id + "'  class='copyright'>"; 
			html = html + "</div>"; 
			_html = $(html); 
		},
		build : function() {			
			this.make(); 
			return $(_html);
		}
	};
}


 


function Button() {
	var _id;
	var _caption;
	var _type;
	var _clickfunc;
	return {
		id : function(id) { 
			if(arguments.length == 0) {
				return _id;
			} else { 
				_id = id; 
				return this;
			} 
		},  
	
		caption : function(caption) { 
			if(arguments.length == 0) {
				return _caption;
			} else { 
				_caption = caption; 
				return this;
			} 
		},
		
		type : function(type) { 
			if(arguments.length == 0) {
				return _type;
			} else { 
				_type = type; 
				return this;
			} 
		}, 
		
		click : function(func) { 
			_clickfunc = func; 
			return this;
		},
 
		build : function() {
			return $("<a>", {
				id : _id,
				html : _caption,
				"class" : "topLink " + _type,
			    click: _clickfunc 
			});			
		}
	};
}


function Popup() {
	var _id;
	var _caption;
	var _type;
	var _clickfunc;
	var _html;
	return {
		id : function(id) { 
			if(arguments.length == 0) {
				return _id;
			} else { 
				_id = id; 
				return this;
			} 
		},  
	
		caption : function(caption) { 
			if(arguments.length == 0) {
				return _caption;
			} else { 
				_caption = caption; 
				return this;
			} 
		},
		
		type : function(type) { 
			if(arguments.length == 0) {
				return _type;
			} else { 
				_type = type; 
				return this;
			} 
		}, 
		
		make : function() {
			var html = ""; 
			html = html + "<div id='" + _id +"' class='popup-container'>";
			html = html +     "<div class='popup-header'>";
			html = html +      _caption; 
			html = html +     "</div>";			
			html = html +     "<div class='popup-content'>123132123123</div>";
			html = html +     "<div class='popup-footer'/>"; 
			html = html + "</div>";   
			_html = $(html);
			_html.hide();  
			    
		},
		
		show : function() {   
			this.build();
			$("#" + _id).remove();
			$("body").append(this.build());
			$(_html).show().css({"z-index" : "1"}).position({my: "center center", of: window}); 
		}, 
	 
		build : function() {
			this.make();
			return $(_html);
		}
	};
	
	 
};

function Grid() { 
	var _data; 
	var _selectedIndex = -1; 
	var _func;
 
	return $.extend(new LeaveObject, { 
		data : function(data) {
			if(arguments.length == 0) {
				return _data;
			} else { 
				_data = data; 
				return this;
			}	 
		},
		
    	selectedIndex : function(selectedIndex) {
			if(arguments.length == 0) {
				return _selectedIndex;
			} else { 
				_selectedIndex = selectedIndex; 
				return this;
			}	
		},
    	
		selectedData : function() {
			return _data[_selectedIndex];
		},
		
		touch : function(func) {
			_func = func;
			return this;
		},
		 
		make : function() {
			var html = ""; 
			html = html + "<div id='" + this.id() +"' class='grid contentinner'>"; 
			html = html +     "<div class='grid-caption'>";
			html = html + this.caption(); 
			html = html + 	  "</div>"; 
		
			$.each(this.data(), function(rowIndex, rowData) {  
				html = html + 	  "<div class='grid-row'>";  
				$.each(rowData, function(columnIndex, columnData) {  
					html = html + 	  "<span  class='grid-column'>";  
					html = html + columnData; 
					html = html + 	  "</span>"; 
				});
				html = html + 	 	 "<span class='grid-column linkmark'>&nbsp;";   
				html = html + 	 	 "</span>"; 
				html = html + 	  "</div>"; 
			});
			html = html + "</div>"; 
			this.html($(html));
		},
		
		build : function() {
			this.make();    
			var index = this.selectedIndex(); 
			var data  = this.selectedData(); 
			this.html().find("div.grid-row").bind("click", function() {
				_selectedIndex = $(this).index();  
				eval(_func)(_selectedIndex,_data[_selectedIndex-1] );
			});   
  
			
			return this.html();
		}		
		
	}); 
}


function TabContainer() { 
	var _tabs = [];
	
	return $.extend(new LeaveObject, { 
		addTab : function(tab) {
			var index = 0; 
			if(_tabs != null) index = _tabs.length;  
			_tabs[index] = tab; 
			return this;
		},
		activeTab : function(tabId) {
			$.each(_tabs, function(index, tab) {
				if(tabId.id() == tabId) {
					tab.active();
				} else {
					tab.inactive();
				} 
			});
		},
		make : function() { 
			var html = "";			
			html = html + "<div id='" + this.id() + "'  class='tabContainer'>"; 
			html = html + 	"<div id='" + this.id() + "'  class='tab'>"; 
			
			html = html + 		"<ul>"; 
			
			$.each(_tabs, function(index, tab) {  
				if(tab.state() == "active") {
					html = html + "<li id='' class='active'>"; 
				} else {
					html = html + "<li>"; 	
				}
				
				html = html + 	"<a href='javascript:void(0);'>" + tab.caption() + "</a>"; 
				html = html + "</li>"; 
			});
			
			html = html + 		"</ul>"; 
			html = html + 	"</div>"; 
			html = html + "</div>"; 
			this.html($(html));
		}, 
		build : function() {			
			this.make(); 
			
			this.html().find(".tab").bind("click", function() {
				 
				
			});
			
			
			return this.html();
		}
	});
}

function TabItem() {  
	var _content;
	var _active = false;
	
	return $.extend(new LeaveObject, {
		content : function(content) {
			_content = content;
		},
		state : function() {
			return _active == true ? "active" : "inactive";
		},
		active : function() {
			_active = true;
			return this;
		},
		inactive : function() {
			_active = false;
			
			this.html().hide();
			return this;
		},
		make : function() {
			var html = "";			
			html = html + "<div id='" + _id + "'  class='tab'>";   
			html = html + "</div>";    
			this.html($(html));
			this.html().append(_content.build());
		}, 
		build : function() {			
			this.make();  
			
			return this.html(); 
		}
	});
}



function SlideupButtonButton() {    
	return { 
			a : function() {}
	}.protype = new Button();
}

(function($) { 
	$.leaveContainer = function() { 
		return new Container();
	}; 
})(jQuery);
(function($) { 
	$.leaveHeader = function() { 
		return new Header();
	}; 
})(jQuery);

(function($) { 
	$.leaveContent = function() { 
		return new Content();
	}; 
})(jQuery);

(function($) { 
	$.leaveFooter = function() { 
		return new Footer();
	}; 
})(jQuery);
(function($) { 
	$.leaveCopyright = function() { 
		return new Copyright();
	}; 
})(jQuery);
(function($) { 
	$.leaveButton = function() { 
		return new Button();
	}; 
})(jQuery); 
(function($) { 
	$.leavePopup = function() {  
		return new Popup(); 
	}; 
})(jQuery);
(function($) { 
	$.leaveGrid = function() {  
		return new Grid();
	}; 
})(jQuery);
(function($) { 
	$.leaveTabContainer = function() {  
		return new TabContainer();
	}; 
})(jQuery);
(function($) { 
	$.leaveTabItem = function() {  
		return new TabItem();
	}; 
})(jQuery);
 